﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using static bmapi.HelperClass.AssistFun;

namespace bmapi.Controllers
{
    public class CAD35Controller : CADFinishController
    {
        [HttpPost, Route("Finish")]
        public string Finish(dynamic json)
        {
             
            if (json == null)
                return ReturnError(201, "参数不能为空");
            else
            {
                #region
                int pcid = Convert.ToInt32(json.pcid);
                int pod = Convert.ToInt32(json.pod);
                int state = Convert.ToInt32(json.state);
                #endregion
                string sql = "update Pod_tb set state=@tate where order_id=@pcid and pod=@pod";

                SQLCommand sc = new SQLCommand(sql);
                sc.Add("@state", state);
                sc.Add("@pcid", pcid);
                sc.Add("@pod", pod);

                int rtn = sc.ExecuteNonQuery();
                if (rtn != 1)
                    return ReturnError(201, "特体处理失败");
                else
                {
                    if (OnUploadPod(pcid))
                        return ReturnError(200, "OK");
                    else
                        return ReturnError(201, "还有特体未处理");
                }
            }
            
            //return ReturnError(200, "OK");
        }

        /// <summary>
        /// orderid订单表中的id== 流程id
        /// </summary> 检查某一个流程的特体是否全部处理完
        /// <param name="orderid"></param>
        /// <param name="lsh"></param>
        /// <returns></returns>
        public bool OnUploadPod(int orderid)
        {
            SQLCommand sc = new SQLCommand("select count(*) from Pod_tb where state=0 and order_id=@orderid", "@orderid", orderid);
            int wait = Convert.ToInt32(sc.ExecuteScalar());
            if (wait > 0)
                return true;
            else
            {
                int rtn=ChangeProcessState(35,orderid);
                if (rtn > 0)
                    return true;
                else
                    return false;
               
            }
        }

        // DELETE: api/CAD35/5
        public void Delete(int id)
        {
        }
    }
}
